<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class admin_user_model extends CI_Model
{

    public function __construct()
    {
        // Call the Model constructor
        parent::__construct();        
    }     

    function login($username, $password)
    {
        $this->db->select('*');
        $this->db->from('landing_user');
        $this->db->where('username',$username);
        $this->db->where('password',MD5($password));        
        $rs = $this->db->get();
    	if ($rs->num_rows() == 0) {
            $rs->free_result();
            return false;
        } else {
            $return = $rs->result_array();            
            $rs->free_result();            
            return $return[0];
        }
    }  
        
	public function isLoggedIn()
	{
		return $this->session->userdata("user_id");			
	}
	public function member_page()
	{
		if($this->isLoggedIn())
		{
			return true;
		}
		else
		{
			redirect(base_url(),"refresh");	
		}
	}	 

	function get_user_by_id($id)
	{
		$this->db->select('*');
        $this->db->from('landing_user');
        $this->db->where('id',$id);               
        $rs = $this->db->get();
    	if ($rs->num_rows() == 0) {
            $rs->free_result();
            return false;
        } else {
            $return = $rs->result_array();            
            $rs->free_result();            
            return $return[0];
        }
	}
	
	function get_user_by_username($username)
	{
		$this->db->select('*');
        $this->db->from('landing_user');
        $this->db->where('username',$username);               
        $rs = $this->db->get();
    	if ($rs->num_rows() == 0) {
            $rs->free_result();
            return false;
        } else {
            $return = $rs->result_array();            
            $rs->free_result();            
            return $return[0];
        }
	}
	
	function get_count_user($status = null){
    	$return = array();
    	$this->db->select('count(*) as user_count')->from('landing_user');
    	if($status){
        	$this->db->where('status',$status);        	
        }  
        $this->db->where('permission !=',1);        
        $rs = $this->db->get();
    	if ($rs->num_rows() == 0) {
            $rs->free_result();
            return false;
        } else {
            $return = $rs->result_array();            
            $rs->free_result();
            return $return;                        
        }                
    }
    
    function get_all_user($start, $limit, $status = null )
    {    	
        $this->db->select('*');
        $this->db->from('landing_user');                    
        if($status){
        	$this->db->where('status',$status);
        }        
        $this->db->where('permission !=',1); 
        $this->db->limit($limit, $start);
        $rs = $this->db->get();
    	if ($rs->num_rows() == 0) {
            $rs->free_result();
            return false;
        } else {
            $return = $rs->result_array();            
            $rs->free_result();            
            return $return;
        }
    }
    
    function set_user($data)
    {    	
    	return ($this->db->insert('landing_user', $data)) ? $this->db->insert_id() : false;
    }
    
	function update_user($data){    	
    	$this->db->where('id', $data['id']);
    	return $this->db->update('landing_user',$data);
    }
    
	function delete_user($id){
   		return $this->db->delete('landing_user',array('id' => $id));
    }		   
   
}

